/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pricecomparison.websitereader.purelandreader;

import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JLabel;
import jxl.write.WriteException;
import pricecomparison.websitereader.WebsiteReader;

/**
 *
 * @author Jim Simon
 */
public class PL_ItemListReader extends PL_HomePageReader {
    String URL = super.URL;
    WebsiteReader reader;
    static Boolean reachedItems = false;

    public PL_ItemListReader(String url) throws java.net.MalformedURLException, java.io.IOException {
        URL += "/" + url;
        try {
            reader = new WebsiteReader(URL);
        } catch (java.net.MalformedURLException e) {
            throw e;
        } catch (java.io.IOException e) {
            throw e;
        }
    }

    public void beginSearch(JLabel l) throws java.net.MalformedURLException, jxl.write.WriteException, java.io.IOException {
        ArrayList<String> items = new ArrayList();

        // Get items
        try {
            items = getItems();
        } catch (java.net.MalformedURLException e) {
            throw e;
        } catch (java.io.IOException e) {
            throw e;
        }

        for (int i = 0; i < items.size(); i++) {
            if (reader.DEBUG)
                System.out.println( "-- Found " + items.get(i));

            l.setText(Integer.toString(Integer.parseInt(l.getText()) + 1));
            
            try {
                PL_ItemReader item = new PL_ItemReader(items.get(i));
                item.beginSearch();
            } catch (java.net.MalformedURLException e) {
                throw e;
            } catch (jxl.write.WriteException e) {
                throw e;
            } catch (java.io.IOException e) {
                throw e;
            }
        }
        
        reachedItems = false;
    }
    
    public ArrayList<String> getPages() throws java.io.IOException {
        ArrayList<String> pages = new ArrayList();
        String line;
        
        try {
            line = reader.readLine();
        } catch (java.io.IOException e) {
            throw e;
        }
        Boolean hasReached = false;
        
        while (line != null) {
            if (line.contains("?pagenum=") && !reachedItems) {
                String key = "\">";
                int pos = line.indexOf(key);
                pages.add(URL.substring(super.URL.length()+1) + line.substring(0, pos));
                
                if (!hasReached)
                    hasReached = true;
            }
            else if (line.contains("</td>") && hasReached)
                break;
            
            try {
                line = reader.readLine();
            } catch (java.io.IOException e) {
                throw e;
            }
        }
        
        // Trim pages
        if (pages.size() > 0)
            pages.remove(pages.size()-1);
        
        return pages;
    }

    private ArrayList<String> getItems() throws java.net.MalformedURLException, java.io.IOException {
        ArrayList<String> links = new ArrayList();
        try {
            reader = new WebsiteReader(URL);
        } catch (java.net.MalformedURLException e) {
            throw e;
        } catch (java.io.IOException e) {
            throw e;
        }
        
        String line;
        try {
            line = reader.readLine();
            
            while (line != null) {
                if(line.contains("<div id=\"Img\">") && line.contains("</a></div>")) {
                    links.add(extractLink(line));
                    if (!reachedItems)
                        reachedItems = true;
                }
                else if (line.contains("<!-- Contents End -->"))
                    break;
                line = reader.readLine();
            }
            
            // Close stream
            reader.close();
        } catch (java.io.IOException e) {
            throw e;
        }
        
        return links;
    }
    
    private String extractPageLink(String line) throws java.io.IOException {
        String nextLine;
        try {
            nextLine = reader.readLine();
        } catch (java.io.IOException e) {
            throw e;
        }
        
        // Find beginning portion
        String key = "href=\"";
        int pos = line.indexOf(key);
        line = line.substring(pos + key.length());
        
        // Find end portion
        key = "\">";
        pos = nextLine.indexOf(key);
        line += nextLine.substring(0, pos);
        
        return line;
    }
}
